package iset.coverageanalyzer.input;

import iset.coverageanalyzer.data.CoverageInfo;

import java.util.ArrayList;

public class JavaTypeSignatureTool {
	
	public static final int START = 0;
	public static final int PRIMITIVE = 1;
	public static final int CLASS = 2;
	public static final int ARRAY = 3;
	public static final int ARRAY_CLASS = 4;
	
	public static int getArgsNum(CoverageInfo ci,String argStr){
		
		int state = JavaTypeSignatureTool.START;
		int argCounter = 0;
		
		for(int i=0; i<argStr.length();i++){
			
			char temp = argStr.charAt(i);
			
			switch(temp){
			case 'Z': 
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing 
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					// do nothing
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'B':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'C':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'S':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'I':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'J':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'F':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'D':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(true));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter ++;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case 'L':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.CLASS;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.CLASS;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing 
					// maybe Error
				}else if(state == JavaTypeSignatureTool.ARRAY){
					state = JavaTypeSignatureTool.ARRAY_CLASS;
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					
				}else{
					// WHAT THE FUCK!
				}
				break;
			case '[':
				if(state == JavaTypeSignatureTool.START){
					state = JavaTypeSignatureTool.ARRAY;
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					state = JavaTypeSignatureTool.ARRAY;
				}else if(state == JavaTypeSignatureTool.CLASS){
					// maybe error
				}else if(state == JavaTypeSignatureTool.ARRAY){
					// maybe error
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					// maybe error
				}else{
					// WHAT THE FUCK!
				}
				break;
			case ';':
				if(state == JavaTypeSignatureTool.START){
					// maybe error
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					// maybe error
				}else if(state == JavaTypeSignatureTool.CLASS){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter++;
				}else if(state == JavaTypeSignatureTool.ARRAY){
					// maybe error
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					state = JavaTypeSignatureTool.PRIMITIVE;
					ci.canBeSolved.add(new Boolean(false));
					argCounter++;
				}else{
					// WHAT THE FUCK!
				}
				break;
			default:
				if(state == JavaTypeSignatureTool.START){
					// do nothing
				}else if(state == JavaTypeSignatureTool.PRIMITIVE){
					// do nothing
				}else if(state == JavaTypeSignatureTool.CLASS){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY){
					// do nothing
				}else if(state == JavaTypeSignatureTool.ARRAY_CLASS){
					// do nothing
				}else{
					// WHAT THE FUCK!
				}
			}
			
		}
		
		return argCounter;
		
	}
	
	// for testing
	public static void main(String[] args){
		CoverageInfo ci = new CoverageInfo(null);
		System.out.println(JavaTypeSignatureTool.getArgsNum(ci,"II"));
		for(int i=0;i<ci.canBeSolved.size();i++){
			System.out.println(ci.canBeSolved.get(i).booleanValue());
		}
		ci.canBeSolved = new ArrayList<Boolean>();
		System.out.println(JavaTypeSignatureTool.getArgsNum(ci,"ILjava/lang/String;[Z[Ljava/lang/String;[I"));
		for(int i=0;i<ci.canBeSolved.size();i++){
			System.out.println(ci.canBeSolved.get(i).booleanValue());
		}
		ci.canBeSolved = new ArrayList<Boolean>();
		System.out.println(JavaTypeSignatureTool.getArgsNum(ci,"I[B[Ljava/lang/String;[III[Z"));
		for(int i=0;i<ci.canBeSolved.size();i++){
			System.out.println(ci.canBeSolved.get(i).booleanValue());
		}
	}
	
}
